<template>
  <el-dialog :visible.sync="poprea" title="不良反应处置" :close-on-click-modal="false" :center="true" v-el-drag-dialog width="830px" max-height="768px">
          <el-form :inline="true" :model="orderInfo" @submit.native.prevent label-width="auto" ref="form2" style="border:1px solid #ccc;">
              <el-card>
                  <div slot="header" class="clearfix">
                      <span>患者信息与输注信息</span>
                  </div>
                  <div>
                      <table style="width:100%">
                          <tr>
                              <td>患者信息</td>
                              <td>
                                  <div v-html="orderInfo.patInfo"></div>
                              </td>
                          </tr>
                          <tr>
                              <td>血袋信息</td>
                              <td>
                                  <div v-html="orderInfo.bagInfo"></div>
                              </td>
                          </tr>
                      </table>
                  </div>
              </el-card>
              <el-card>
                  <div slot="header" class="clearfix">
                      <span>登记信息</span>
                  </div>
                  <div>
                      <div v-html="orderInfo.signInfo"></div>
                  </div>
              </el-card>
              <el-card>
                  <div slot="header" class="clearfix">
                      <span>临床处置信息</span>
                  </div>
                  <div>
                      <div v-html="orderInfo.dealInfo"></div>
                  </div>
              </el-card>
              <el-card>
                  <div slot="header" class="clearfix">
                      <span>输血科处置</span>
                  </div>
                  <div v-if ="listbysort ==false">
                      <table style="width:100%">
                          <tr v-show="orderInfo.bdchecks!=undefined && orderInfo.bdchecks.length>0">
                              <td style="width:53px"></td>
                              <td>
                                  <div v-for="item in orderInfo.bdchecks" :key="item.code" style="display:inline-block;padding-right:5px">
                                      <el-checkbox v-model="item.value" true-label="1" false-label="0"></el-checkbox> {{item.label}}
                                  </div>
                              </td>
                          </tr>
                      </table>
                      <table style="width:100%">
                          <tr v-for="item in orderInfo.bdcinputs" :key="item.code">                                
                              <td>
                                  <div style="text-align:left;font-weight: bold;">
                                      {{item.label}}
                                  </div>
                                  <div v-if="item.type==1">
                                      <el-input type="text"  v-model="item.value"></el-input>
                                  </div>
                                  <div v-if="item.type==2">
                                      <el-select style="width:100%;" v-model="item.value" ref="cflag">
                                          <el-option v-for="itemo in item.options" :key="itemo.codeno" :value="itemo.codeno" :label="itemo.codena"></el-option>
                                      </el-select>
                                  </div>
                                  <div v-if="item.type==3">
                                      <el-input type="textarea" :rows="3"  v-model="item.value"></el-input>
                                  </div>
                              </td>
                          </tr>
                      </table>
                          
                      <table style="width:100%;margin-top:3px">
                          <tr v-for="item in orderInfo.bdinputs" :key="item.code">
                              <td style="width:53px">{{item.label}}</td>
                              <td>
                                  <div v-if="item.type==1">
                                      <el-input type="text"  v-model="item.value"></el-input>
                                  </div>
                                  <div v-if="item.type==2">
                                      <el-select style="width:100%;" v-model="item.value" ref="cflag">
                                          <el-option v-for="itemo in item.options" :key="itemo.codeno" :value="itemo.codeno" :label="itemo.codena"></el-option>
                                      </el-select>
                                  </div>
                                  <div v-if="item.type==3">
                                      <el-input type="textarea" :rows="3"  v-model="item.value"></el-input>
                                  </div>
                              </td>
                          </tr>
                      </table>
                  </div>
                  <div v-if ="listbysort " style="height:300px" >
                      <vxe-table    :data="orderInfo.bditemlist"     :show-header="false"         border show-header-overflow    stripe fit height="auto"
                      highlight-current-row highlight-hover-column   ref="reaitemlist" resizable  size="mini" tableid ="NBSREA_LIST"  :span-method="RowspanMethod">
                          <vxe-table-column field="group"  title="分组" width="80"  align="left">
                          </vxe-table-column>
                          <vxe-table-column field="label"  title="项目"   width="350" align="left">
                          </vxe-table-column>
                          <vxe-table-column  field="value"   title="结果"   width="320" align="left">
                              <template v-slot="{row}">                                     
                                  <el-checkbox v-model="row.value" true-label="1" false-label="0"  v-if="row.type==0"> {{row.label}}</el-checkbox>
                                  <el-input type="text"  v-model="row.value" v-if="row.type==1"></el-input>
                                  <el-select style="width:100%;" v-model="row.value"  v-if="row.type==2">
                                                  <el-option v-for="itemo in row.options" :key="itemo.codeno" :value="itemo.codeno" :label="itemo.codena"></el-option>
                                  </el-select>
                                  <el-input type="textarea" :rows="3"  v-if="row.type==3" v-model="row.value"></el-input>
                              </template>
                          </vxe-table-column>

                      <!--table style="width:100%;border-spacing:0px;border:1px solid #ccc" >
                               <tr v-for="item in orderInfo.bditemlist" :key="item.code"  >
                                      <td style="width:80px"  >{{ item.group}}</td>
                                      <td style="width:400px">{{item.label}}</td> 
                                      <td style="width:350px">
                                          <div v-if="item.type==0">
                                              <el-checkbox v-model="item.value" true-label="1" false-label="0"></el-checkbox> {{item.label}}
                                           </div>
                                           <div v-if="item.type==1">
                                      <el-input type="text"  v-model="item.value"></el-input>
                                          </div>
                                          <div v-if="item.type==2">
                                              <el-select style="width:100%;" v-model="item.value" ref="cflag">
                                                  <el-option v-for="itemo in item.options" :key="itemo.codeno" :value="itemo.codeno" :label="itemo.codena"></el-option>
                                              </el-select>
                                          </div>
                                          <div v-if="item.type==3">
                                              <el-input type="textarea" :rows="3"  v-model="item.value"></el-input>
                                          </div>
                                      </td>

                              </tr>
                      </table-->
                      </vxe-table>
                  </div>     
              </el-card>
              
          </el-form>
          <div style="text-align:right;padding-top:5px">
              <el-button v-show="this.orderInfo.dealByDoctor=='1'" @click="backDoctor()" type="warning">退回临床重新处置</el-button>
              <el-button @click="rea(0)">保存</el-button>
              <el-button @click="rea(1)">提交</el-button>
              <el-button @click="poprea=false">取消</el-button>
          </div>
      </el-dialog>
</template>

<script>

export default{
  name: "NBS513",
  data() {
      return {
          reaUser:'',
          poprea:false,
          orderInfo:{},
          listbysort:false
      };
  },
  methods: {
      backDoctor(){
          //弹出用户验证框
          this.$rm_Login({
                  type: "请输入退回临床重新处置工号和密码",
                  btn: {
                      confirmBtnText: "确定",
                      cancelBtnText: "取消"
                  }
              })
              .then(res => {
                  if (res.flag == "1") {
                      this.$tool
                          .request({
                              url: "NBS/NBS5/NBS513",
                              method: "post",
                              data: {
                                  action: "backdoctor",
                                  orderid:this.orderInfo.orderid,
                                  userno:res.userid,
                                  msgid:this.orderInfo.msgid,
                                  reactionid:this.orderInfo.reactionid
                              }
                          })
                          .then(res => {
                              if(res.errcode=='1'){
                                  this.$message.success("退回临床重新处置成功！");
                                  this.$emit('afterRea',0);
                                  this.poprea=false;
                              }
                              else{
                                  this.$alert('退回临床重新处置失败：'+res.errmsg+'！', "错误", {
                                      confirmButtonText: "确定"
                                  });
                              }
                          })
                          .catch(err => {
                              this.$message.error("错误:" + err);
                          });
                  } else {
                      //登录失败
                  }
              })
              .catch(err => {
                  this.$message.error("错误:" + err);
              });
      },
      rea(flag){
          if(this.reaUser=='' && flag==1){
              //弹出用户验证框
              this.$rm_Login({
                      type: "请输入处置医生工号和密码",
                      btn: {
                          confirmBtnText: "确定",
                          cancelBtnText: "取消"
                      }
                  })
                  .then(res => {
                      if (res.flag == "1") {
                          this.reaUser=res.userid;
                          this.$emit('setUser',res.userid,res.username);
                          this.reaOrder(flag);
                      } else {
                          //登录失败
                      }
                  })
                  .catch(err => {
                      //this.$message.error("错误:" + err);
                  });
          }
          else{
              this.reaOrder(flag);
          }
          
      },
      RowspanMethod({ row, $rowIndex, column, data }) {
        //  let fields = ["matchdate", "sampleno", "matchid"];
        //  let fileds1= ["bloodcode","compname","abo","rh","spec","unit","outflag","ecmflag"];
          let cellValue = row[column.property]; // 列属性
         let _group = row["group"];
        //  let  _bldid = row["bld_id"];
          if (cellValue && column.property =="group") {
              let prevRow = data[$rowIndex - 1];
              let nextRow = data[$rowIndex + 1];
              if (prevRow && prevRow[column.property] === cellValue && prevRow["group"] === _group ) {
                  return { rowspan: 0, colspan: 0 };
              } else {
                  let countRowspan = 1;
                  while (nextRow && nextRow[column.property] === cellValue && nextRow["group"] === _group) {
                      //&& row["matchid"] === nextRow["matchid"]
                      nextRow = data[++countRowspan + $rowIndex];
                  }
                  if (countRowspan > 1) {
                      return { rowspan: countRowspan, colspan: 1 };
                  }
              }
          }
      },
      reaOrder(flag){
          var that=this;
          console.log(that.orderInfo);
          this.$tool
              .request({
                  url: "NBS/NBS5/NBS513",
                  method: "post",
                  data: {
                      action: "rea",
                      orderid:that.orderInfo.orderid,
                      flag:flag,
                      userno:that.reaUser,
                      msgid:that.orderInfo.msgid,
                      reactionid:that.orderInfo.reactionid,
                      bdchecks:that.orderInfo.bdchecks,
                      bdinputs:that.orderInfo.bdinputs,
                      bdcinputs:that.orderInfo.bdcinputs,
                      
                  }
              })
              .then(res => {
                  if(res.errcode=='1'){
                      this.$message.success(flag==0?"保存成功！":"提交成功！");
                      
                      if(flag==1){
                          this.poprea=false;
                          this.$emit('afterRea',1);
                      }
                      
                  }
                  else{
                      this.$alert('保存失败：'+res.errmsg+'！', "错误", {
                          confirmButtonText: "确定"
                      });
                  }
              })
              .catch(err => {
                   this.$message.error("错误:" + err);
              });
          
      },
      openrea(orderid,msgid,reactionid,userno) {
          this.reaUser=userno;
          var that=this;
          this.$tool
              .request({
                  url: "NBS/NBS5/NBS513",
                  method: "post",
                  data: {
                      action: "getorderinfo",
                      orderid:orderid,
                      msgid:msgid,
                      reactionid:reactionid                       
                  }
              })
              .then(res => {
                  that.orderInfo = res;
                  that.listbysort = (res.listbysort =="1");
                  that.poprea=true;
              })
              .catch(err => {
                  this.$message.error("错误:" + err);
              });
      },
  }
}
</script>
<style  scoped>
::v-deep  div.el-card__body
{
  padding: 0px 0px 0px 0px;
}
::v-deep   .vxe-table .vxe-cell
{
  text-align: left !important;
}
.itemlist  tr td:nth-child(odd) {
  text-align: left !important;
  white-space: nowrap;
  padding-left:5px;
}

</style>
